class Solution
{
public:
    int numberOfSpecialChars(string word)
    {
        vector<int> lowerPos(26, -1);
        vector<int> upperPos(26, -1);
        int n = word.size();
        for (int i = 0; i < n; ++i)
        {
            char c = word[i];
            if (c >= 'a' && c <= 'z')
            {
                lowerPos[c - 'a'] = i;
            }
            else
            {
                if (upperPos[c - 'A'] == -1)
                {
                    upperPos[c - 'A'] = i;
                }
            }
        }

        int result = 0;
        for (int i = 0; i < 26; ++i)
        {
            if (lowerPos[i] != -1 && upperPos[i] != -1 && lowerPos[i] < upperPos[i])
            {
                ++result;
            }
        }
        return result;
    }
};